{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8dde0bc6-ca7d-41cc-83d2-c1cb0d6f39e3",
   "metadata": {},
   "source": [
    "# 一、python基础\n",
    "## 1.1、python的输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bfcd7623-0824-4967-9c07-eb6b9c46c861",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17700564-2a9f-47a8-86ad-0484f6132caa",
   "metadata": {},
   "source": [
    "## 1.2、内存的驻留"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d069bcd2-6976-4620-b5cd-a1dc42c244b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140737065665736\n",
      "140737065666056\n",
      "140737065665736\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(id(a))\n",
    "a = 20\n",
    "print(id(a))\n",
    "a = 10\n",
    "print(id(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4d39bdee-94d0-44c7-afd4-2433f0f89197",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140737065730016\n",
      "140737065730064\n",
      "140737065730016\n"
     ]
    }
   ],
   "source": [
    "a = 'a'\n",
    "print(id(a))\n",
    "a = 'b'\n",
    "print(id(a))\n",
    "a = 'a'\n",
    "print(id(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c2638b27-48c4-41fc-b089-8ec9c80c4a01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "152700448\n",
      "140737065730064\n",
      "152706208\n"
     ]
    }
   ],
   "source": [
    "a = 'a一二666'\n",
    "print(id(a))\n",
    "a = 'b'\n",
    "print(id(a))\n",
    "a = 'a一二666'\n",
    "print(id(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b5ff8570-a85b-4a6f-be48-a9f93fc4654b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.30000000000000004\n"
     ]
    }
   ],
   "source": [
    "a = 0.1\n",
    "b = 0.2\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "63ee2c70-bf53-4e32-a4f9-3fd0fa8e3abd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3\n"
     ]
    }
   ],
   "source": [
    "from decimal import Decimal as a\n",
    "print(a('0.1')+a('0.2'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca369b73-ce6a-4f5d-a2b5-bc966c224c0f",
   "metadata": {},
   "source": [
    "### 1.2.1格式化字符串\n",
    "\n",
    "格式化字符串有如下三种方式:\n",
    "\n",
    "- `%` 格式化\n",
    "- `str.format()`\n",
    "- f-string（推荐，Python3.6+）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bdc6538f-e22d-45b9-ae5e-9bf210d15cae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫张三,今年22岁\n",
      "我叫张三,今年22岁\n",
      "我叫张三,今年22岁\n"
     ]
    }
   ],
   "source": [
    "name = \"张三\"\n",
    "age = 22\n",
    "# print(\"我叫\"+name+\",今年\"+age+\"岁\")\n",
    "print(\"我叫%s,今年%d岁\"%(name,age))\n",
    "print(\"我叫{},今年{}岁\".format(name,age))\n",
    "print(f\"我叫{name},今年{age}岁\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "692c2fae-3f27-4985-8314-ac88dfd8515d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入姓名 czl\n",
      "输入年龄 17\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你叫czl，今年17岁\n"
     ]
    }
   ],
   "source": [
    "name = input('输入姓名')\n",
    "age = int(input('输入年龄'))\n",
    "print(\"你叫{}，今年{}岁\".format(name,str(age)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06fe4530-0c09-44e5-80ab-3c8b469cd1a8",
   "metadata": {},
   "source": [
    "## 1.3、运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bf22bacf-dd23-4260-8616-4741da08b896",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "嘞！！！\n",
      "嘞！！！\n",
      "嘞！！！\n",
      "嘞！！！\n",
      "嘞！！！\n",
      "嘞！！！\n",
      "嘞！！！\n",
      "嘞！！！\n",
      "嘞！！！\n",
      "嘞！！！\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('嘞！！！\\n'*10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95010b44-2647-43c5-835d-0d094225a8db",
   "metadata": {},
   "source": [
    "## 1.4、对象的bool值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9032a9ce-6ac3-41c2-9d32-69d14deadd00",
   "metadata": {},
   "source": [
    "空的和0都是false，其余都是true,包括-1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8af738ef-c330-466e-8659-d65b500a7a8f",
   "metadata": {},
   "source": [
    "## 1.5、选择结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d26cf67a-a0cb-496f-a229-15defd8dc660",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入成绩 100\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n"
     ]
    }
   ],
   "source": [
    "a = int(input('输入成绩'))\n",
    "if a >= 90:\n",
    "    print(\"A\")\n",
    "elif a >= 80:\n",
    "    print('B')\n",
    "elif a >= 70:\n",
    "    print('C')\n",
    "elif a >= 60:\n",
    "    print('D')\n",
    "else:\n",
    "    print('丸蛋辣！')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1c5149d8-2714-4a65-8442-c907cb94e949",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入取钱数 12345\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你取出了12345元，剩下102169元\n"
     ]
    }
   ],
   "source": [
    "a = 114514\n",
    "b = int(input('输入取钱数'))\n",
    "if b < 0:\n",
    "    print('你没有负的钱！')\n",
    "elif b == 0:\n",
    "    print('你没有取钱！')\n",
    "elif b > a:\n",
    "    print('你没有这么多钱！')\n",
    "else:\n",
    "    a -= b\n",
    "    print('你取出了{}元，剩下{}元'.format(b,a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfc2167d-bb9c-478c-8fd3-3344847a22f0",
   "metadata": {},
   "source": [
    "## 1.6、循环结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4cda73e3-5d94-445e-b5cf-4c051843f8ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "165175120 <class 'range'> 48\n",
      "165180304 <class 'range'> 48\n",
      "0 1 2 3 4 5 6 7 8 9 "
     ]
    }
   ],
   "source": [
    "import sys\n",
    "r = range(10) #0~9的迭代器\n",
    "print(id(r),type(r),sys.getsizeof(r))\n",
    "#左闭右开\n",
    "r1 = range(200,220)\n",
    "print(id(r1),type(r1),sys.getsizeof(r1))\n",
    "for i in r:\n",
    "    print(i,end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e37e2e72-24a0-4c4f-8bee-aff3e70d6929",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "666\n",
      "666\n",
      "666\n",
      "666\n",
      "666\n",
      "666\n",
      "666\n",
      "666\n",
      "666\n",
      "666\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print('666')\n",
    "#创建了10个变量i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "68fb4180-e134-4dfe-9cf9-6d3edd214543",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "196020\n"
     ]
    }
   ],
   "source": [
    "num = 0;\n",
    "for i in range(80,889,2):\n",
    "    num += i\n",
    "print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c269e3a9-e820-4241-a311-6ee9c71dc621",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1x1=1\t\n",
      "1x2=2\t2x2=4\t\n",
      "1x3=3\t2x3=6\t3x3=9\t\n",
      "1x4=4\t2x4=8\t3x4=12\t4x4=16\t\n",
      "1x5=5\t2x5=10\t3x5=15\t4x5=20\t5x5=25\t\n",
      "1x6=6\t2x6=12\t3x6=18\t4x6=24\t5x6=30\t6x6=36\t\n",
      "1x7=7\t2x7=14\t3x7=21\t4x7=28\t5x7=35\t6x7=42\t7x7=49\t\n",
      "1x8=8\t2x8=16\t3x8=24\t4x8=32\t5x8=40\t6x8=48\t7x8=56\t8x8=64\t\n",
      "1x9=9\t2x9=18\t3x9=27\t4x9=36\t5x9=45\t6x9=54\t7x9=63\t8x9=72\t9x9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10,1):\n",
    "    for j in range(1,i+1,1):\n",
    "        print('{}x{}={}'.format(j,i,i*j),end='\\t')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "b2924912-49c5-4812-91c0-9c7b710fd0df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       *\n",
      "\n",
      "      ***\n",
      "\n",
      "     *****\n",
      "\n",
      "    *******\n",
      "\n",
      "   *********\n",
      "\n",
      "  ***********\n",
      "\n",
      " *************\n",
      "\n",
      "       *\n",
      "       *\n",
      "       *\n",
      "       *\n",
      "       *\n",
      "       *\n",
      "       *\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "l = 7\n",
    "for i in range(l):\n",
    "    print(' '*(l-i-1),'*'*a)\n",
    "    a += 2\n",
    "    print()\n",
    "a = 1\n",
    "for i in range(l):\n",
    "    print(' '*(l-1),'*')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "f287ded8-e60f-4c2d-9db6-e3f840040414",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "end！\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(i)\n",
    "else:\n",
    "    print('end！') #循环结束执行else，不正常结束，如break，不执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "f2d2f543-7d95-4823-833a-bfe25b6040a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入密码，你还有3次机会 114514\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "密码错误！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入密码，你还有2次机会 lth\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "密码错误！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入密码，你还有1次机会 114514lth\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "密码正确！\n"
     ]
    }
   ],
   "source": [
    "ps = '114514lth'\n",
    "trys = 3\n",
    "for i in range(trys,0,-1):\n",
    "    a = input('输入密码，你还有{}次机会'.format(i))\n",
    "    if a == ps:\n",
    "        print('密码正确！')\n",
    "        break;\n",
    "    else:\n",
    "        print(\"密码错误！\")\n",
    "else:\n",
    "    print('密码错误3次，账户已锁定')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18cc83f5-2c59-467c-b21a-616660e9edc3",
   "metadata": {},
   "source": [
    "# 二、python的集合类 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bdf764c-ac46-4462-901c-59f5c85d5245",
   "metadata": {},
   "source": [
    "## 2.1、列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "cc7ce6a5-020c-4df7-bb1d-d33fb3dcfaa2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "[1, 1, 4, 5, 1, 4, '臭', 'l', 't', 'h', True]\n",
      "1 1 4 5 1 4 臭 l t h True \n",
      "94575424 <class 'list'>\n",
      "140734910120968 <class 'int'>\n",
      "140734910185968 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "lst = [1,1,4,5,1,4,'臭','l','t','h',True]\n",
    "print(lst[3])\n",
    "print(lst)\n",
    "for i in lst:\n",
    "    print(i,end=' ')\n",
    "print()\n",
    "print(id(lst),type(lst))\n",
    "print(id(lst[2]),type(lst[2]))\n",
    "print(id(lst[7]),type(lst[7]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3db50ac7-bdea-45e6-8441-2028e0ea697a",
   "metadata": {},
   "source": [
    "### 2.1.1、list的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "1b8534a0-cec3-45a3-a6c6-e5908725ea40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 136\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]\n",
      "[0, 2, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "lst = list([1,2,3,4])\n",
    "print(lst)\n",
    "lst = list(range(10,20))\n",
    "import sys\n",
    "print(lst,sys.getsizeof(lst))\n",
    "lst2 = [i for i in range(10)]\n",
    "print(lst2)\n",
    "lst2 = [i**3 for i in range(10)]\n",
    "print(lst2)\n",
    "lst2 = [i for i in range(10) if i%2==0]\n",
    "print(lst2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cb5d95f-21db-4bf4-bed6-8c4948b5bb36",
   "metadata": {},
   "source": [
    "### 2.1.2、list的切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "2f56bcec-1746-4af0-9572-c1262b8dd1e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[1, 3, 5]\n",
      "[3, 6]\n",
      "[2, 3]\n",
      "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n",
      "[8, 6, 4, 2, 0]\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(10))\n",
    "print(lst)\n",
    "#切片：lst[start:end:step]，左开右闭\n",
    "print(lst[1:6:2])\n",
    "print(lst[3:8:3])\n",
    "print(lst[2:4:1])\n",
    "print(lst[::-1])\n",
    "print(lst[-2S::-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b1dd742-84ad-40e2-8c22-c05010e028a7",
   "metadata": {},
   "source": [
    "列表中提取奇数位元素并且反向输出\n",
    "奇数和偶数交换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "17df8b72-f9cd-4a1b-a7ce-70d42b00cf31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11, 13, 15, 17, 19]\n",
      "[19, 17, 15, 13, 11]\n",
      "[19, 17, 15, 13, 11]\n",
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(10,20))\n",
    "l = lst[1::2]\n",
    "print(l)\n",
    "print(l[::-1])\n",
    "print(lst[1::2][::-1]) #原list不变\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bfc13ed-7dc5-4f66-9c6e-2a91e8d73470",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = list(range(10,20))\n",
    "l1 = lst[1::2]\n",
    "l2 = lst[::2]\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c251cb0-ad02-451b-aa4d-f6e09918b1ac",
   "metadata": {},
   "source": [
    "### 2.1.3、list操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "97de613e-83b0-435c-88fd-a906f6f2367a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9] 93123520\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 11] 93123520\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(1,10))\n",
    "print(lst,id(lst))\n",
    "lst.append(11)\n",
    "print(lst,id(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "c5fa4739-af64-4b03-ad4c-c31099f05a21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9] 127615488\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 11] 127615488\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(1,10))\n",
    "print(lst,id(lst))\n",
    "lst2 = lst\n",
    "lst2.append(11)\n",
    "print(lst,id(lst))\n",
    "# lst2指向lst，lst为可变对象，被同时改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "2e3b286d-da07-4ba0-943b-b5e9467a3417",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9] 93010752\n",
      "[1, 4, 5, 6, 7, 8] 93010752\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(1,10))\n",
    "print(lst,id(lst))\n",
    "lst.pop() #删除最后一位\n",
    "lst.pop(2) #删除某一位置\n",
    "lst.remove(2) #删除值为某一的元素，若删除元素不存在会报错\n",
    "print(lst,id(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcfa57e4-07fc-4f5a-b3dc-2bc44a6d7f60",
   "metadata": {},
   "source": [
    "### 2.1.4、list插入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "14753123-2207-4958-8a3b-e0cbbd65bbdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 22, 2, 3]\n",
      "[0, 1, [21, 31, 41], 22, 2, 3]\n",
      "[0, 222, 333, 444]\n",
      "[0, 222, 22, 33, 44]\n",
      "[0, 1, 222, 333, 444, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "lst = [0,1,2,3]\n",
    "lst.insert(2,22) #在位置2后面插入22\n",
    "print(lst)\n",
    "lst.insert(2,[21,31,41])\n",
    "print(lst)\n",
    "lst[1:] = [222,333,444] #删除第1个位置后面的元素并插入\n",
    "print(lst)\n",
    "lst[2:] = [22,33,44] #删除第2个位置后面的元素并插入\n",
    "print(lst)\n",
    "lst = list(range(10))\n",
    "lst[2:5] = [222,333,444] #删除第2个位置后面的元素直到第5个并插入(2,5]\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "d3cdb718-8143-40e6-809d-e4e9fdd4c3f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(10))\n",
    "print(lst)\n",
    "print(lst.index(0)) #查找下标，从0开始"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5758db6-5a69-432f-94d9-382569d68e06",
   "metadata": {},
   "source": [
    "- 下标=位置-1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fe389e3-97d9-42c0-8593-79a37b6e6d0e",
   "metadata": {},
   "source": [
    "## 2.2、字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "ba885d93-e5ae-46eb-9393-8510a8193a0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'e': 20}\n",
      "{'a': 10, 'e': 20, 'aaa': 99}\n"
     ]
    }
   ],
   "source": [
    "m = {'a':1,'e':20}\n",
    "print(m)\n",
    "m['a'] = 10\n",
    "m['aaa'] = 99\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "6c928603-b522-44e6-b471-7f51c65e5b39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "m = {'a':1,'e':20}\n",
    "b = 'aaa' in m\n",
    "print(b)\n",
    "c = 'a' in m\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "84fb3176-82e8-4b41-9e40-775225c8a53a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e': 20}\n"
     ]
    }
   ],
   "source": [
    "m = {'a':1,'e':20}\n",
    "m.pop('a') #空着则是删除最后一位的键值对\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ec5ff6a-a692-48b9-8039-cda4d6b5eeb8",
   "metadata": {},
   "source": [
    "#### 补充：格式化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "9821d577-5198-4707-849b-efea789b5d87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：       abc\n"
     ]
    }
   ],
   "source": [
    "name = \"abc\"\n",
    "print('姓名：{:>10}'.format(name)) \n",
    "#{:符号数字}符号<左对齐，>右对齐，^居中，数字表示占位多少。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "8a038242-57d6-452a-ac52-932f90a95e11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaa     bbb     ccc     ddd     \n",
      "天才啊     bbb     ccc     ddd     \n",
      "12      芜湖      ccc     ddd     \n"
     ]
    }
   ],
   "source": [
    "f = \"{:<8}{:<8}{:<8}{:<8}\"\n",
    "print(f.format(\"aaa\",\"bbb\",\"ccc\",\"ddd\"))\n",
    "print(f.format(\"天才啊\",\"bbb\",\"ccc\",\"ddd\"))\n",
    "print(f.format(\"12\",\"芜湖\",\"ccc\",\"ddd\"))\n",
    "#剩下的长度自己对齐"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ea3646c-2da8-4f91-bc40-578aad120be4",
   "metadata": {},
   "source": [
    "## 2.3、学生管理系统"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "c807a599-43f4-43fc-acf8-bf8d43c93bc5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "welcome!\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 a\n",
      "输入学号 1\n",
      "输入姓名 gfd\n",
      "输入年龄 8\n",
      "输入专业 zcxc\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加成功\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 l\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号      姓名      年龄      专业      \n",
      "1         gfd     8         zcxc    \n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 u\n",
      "输入要更改的学号 1\n",
      "输入姓名 ggg\n",
      "输入年龄 90\n",
      "输入专业 vvv\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更改成功！\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 l\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号      姓名      年龄      专业      \n",
      "1         ggg     90        vvv     \n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 e\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bye\n"
     ]
    }
   ],
   "source": [
    "f_title = \"{:<8}{:<8}{:<8}{:<8}\"\n",
    "f_data = \"{:<10}{:<8}{:<10}{:<8}\"\n",
    "sts = []\n",
    "\n",
    "def addstu():\n",
    "    nu = int(input(\"输入学号\"))\n",
    "    name = input(\"输入姓名\")\n",
    "    age = int(input(\"输入年龄\"))\n",
    "    ob = input(\"输入专业\")\n",
    "    sts.append({'no':nu,'name':name,'age':age,'major':ob})\n",
    "    print('添加成功')\n",
    "\n",
    "def delestu():\n",
    "    nu = int(input(\"输入要删除的学号\"))\n",
    "    stu = None\n",
    "    for s in sts:\n",
    "        if s['no'] == nu:\n",
    "            stu = s\n",
    "            sts.remove(stu)\n",
    "            print('删除成功！')\n",
    "            break;\n",
    "    else:\n",
    "        print('学生不存在！')\n",
    "\n",
    "def seastu():\n",
    "    nu = int(input('输入要查询的学号'))\n",
    "    stu = None\n",
    "    for s in sts:\n",
    "        if s['no'] == nu:\n",
    "            print(f_title.format('学号','姓名','年龄','专业'))\n",
    "            print(f_data.format(s['no'],s['name'],s['age'],s['major']))\n",
    "            break;\n",
    "    else:\n",
    "        print(\"学生不存在！\")\n",
    "\n",
    "def cgstu():\n",
    "    nu = int(input('输入要更改的学号'))\n",
    "    stu = None\n",
    "    for s in sts:\n",
    "        if s['no'] == nu:\n",
    "            stu = s\n",
    "            sts.remove(stu)\n",
    "            name = input(\"输入姓名\")\n",
    "            age = int(input(\"输入年龄\"))\n",
    "            ob = input(\"输入专业\")\n",
    "            sts.append({'no':nu,'name':name,'age':age,'major':ob})\n",
    "            print('更改成功！')\n",
    "            break;\n",
    "    else:\n",
    "        print(\"学生不存在！\")\n",
    "\n",
    "def main():\n",
    "    print('welcome!')\n",
    "    while True:\n",
    "        print('选择操作：添加A,删除D,更新U,查询Q,列表L,退出E')\n",
    "        op = input(\"输入\")\n",
    "        op = op.upper()\n",
    "        if op == 'A':\n",
    "            addstu()\n",
    "        elif op == 'D':\n",
    "            delestu()\n",
    "        elif op == 'U':\n",
    "            cgstu()\n",
    "        elif op == 'Q':\n",
    "            seastu()\n",
    "        elif op == 'L':\n",
    "            print(f_title.format('学号','姓名','年龄','专业'))\n",
    "            for s in sts:\n",
    "                print(f_data.format(s['no'],s['name'],s['age'],s['major']))\n",
    "        elif op == 'E':\n",
    "            print(\"bye\")\n",
    "            break;\n",
    "        else:\n",
    "            print('选择正确操作！')\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d1c1f05-36ba-436b-9e36-f6bdaf22e667",
   "metadata": {},
   "source": [
    "## 2.4、员工管理系统"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e1e61c7d-d5de-463c-a0dc-e8d3a4d32f6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "welcome!\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "选择正确操作！\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 a\n",
      "输入工号 1\n",
      "输入姓名 a\n",
      "输入年龄 1\n",
      "输入岗位 a\n",
      "输入薪水 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加成功\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 a\n",
      "输入工号 2\n",
      "输入姓名 b\n",
      "输入年龄 2\n",
      "输入岗位 b\n",
      "输入薪水 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加成功\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 a\n",
      "输入工号 3\n",
      "输入姓名 c\n",
      "输入年龄 3\n",
      "输入岗位 c\n",
      "输入薪水 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加成功\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 l\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "工号      姓名      年龄      岗位      薪水      \n",
      "1         a       1         a       1       \n",
      "2         b       2         b       2       \n",
      "3         c       3         c       3       \n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 u\n",
      "输入要更改的工号 2\n",
      "输入姓名 b\n",
      "输入年龄 2\n",
      "输入岗位 b\n",
      "输入薪水 22\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更改成功！\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 q\n",
      "输入要查询的工号 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "工号      姓名      年龄      岗位      薪水      \n",
      "2         b       2         b       22      \n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 d\n",
      "输入要删除的工号 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除成功！\n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 l\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "工号      姓名      年龄      岗位      薪水      \n",
      "1         a       1         a       1       \n",
      "3         c       3         c       3       \n",
      "选择操作：添加A,删除D,更新U,查询Q,列表L,退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 e\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bye\n"
     ]
    }
   ],
   "source": [
    "f_title = \"{:<8}{:<8}{:<8}{:<8}{:<8}\"\n",
    "f_data = \"{:<10}{:<8}{:<10}{:<8}{:<8}\"\n",
    "emps = []\n",
    "\n",
    "def addemp():\n",
    "    nu = int(input(\"输入工号\"))\n",
    "    em = None\n",
    "    for e in emps:\n",
    "        if e['no'] == nu:\n",
    "            print(\"员工已存在！\")\n",
    "            break;\n",
    "    else:\n",
    "        name = input(\"输入姓名\")\n",
    "        age = int(input(\"输入年龄\"))\n",
    "        ob = input(\"输入岗位\")\n",
    "        mo = int(input(\"输入薪水\"))\n",
    "        emps.append({'no':nu,'name':name,'age':age,'major':ob,'salary':mo})\n",
    "        print('添加成功')\n",
    "\n",
    "def delemp():\n",
    "    nu = int(input(\"输入要删除的工号\"))\n",
    "    em = None\n",
    "    for e in emps:\n",
    "        if e['no'] == nu:\n",
    "            em = e\n",
    "            emps.remove(em)\n",
    "            print('删除成功！')\n",
    "            break;\n",
    "    else:\n",
    "        print('员工不存在！')\n",
    "\n",
    "def searchemp():\n",
    "    nu = int(input('输入要查询的工号'))\n",
    "    em = None\n",
    "    for e in emps:\n",
    "        if e['no'] == nu:\n",
    "            print(f_title.format('工号','姓名','年龄','岗位','薪水'))\n",
    "            print(f_data.format(e['no'],e['name'],e['age'],e['major'],e['salary']))\n",
    "            break;\n",
    "    else:\n",
    "        print(\"员工不存在！\")\n",
    "\n",
    "def changeemp():\n",
    "    nu = int(input('输入要更改的工号'))\n",
    "    em = None\n",
    "    for e in emps:\n",
    "        if e['no'] == nu:\n",
    "            em = e\n",
    "            emps.remove(em)\n",
    "            name = input(\"输入姓名\")\n",
    "            age = int(input(\"输入年龄\"))\n",
    "            ob = input(\"输入岗位\")\n",
    "            mo = int(input(\"输入薪水\"))\n",
    "            emps.append({'no':nu,'name':name,'age':age,'major':ob,'salary':mo})\n",
    "            print('更改成功！')\n",
    "            break;\n",
    "    else:\n",
    "        print(\"员工不存在！\")\n",
    "\n",
    "def listemp():\n",
    "    print(f_title.format('工号','姓名','年龄','岗位','薪水'))\n",
    "    for e in emps:\n",
    "        print(f_data.format(e['no'],e['name'],e['age'],e['major'],e['salary']))\n",
    "\n",
    "def main():\n",
    "    print('welcome!')\n",
    "    while True:\n",
    "        print('选择操作：添加A,删除D,更新U,查询Q,列表L,退出E')\n",
    "        op = input(\"输入\")\n",
    "        op = op.upper()\n",
    "        if op == 'A':\n",
    "            addemp()\n",
    "        elif op == 'D':\n",
    "            delemp()\n",
    "        elif op == 'U':\n",
    "            changeemp()\n",
    "        elif op == 'Q':\n",
    "            searchemp()\n",
    "        elif op == 'L':\n",
    "            listemp()\n",
    "        elif op == 'E':\n",
    "            print(\"bye\")\n",
    "            break;\n",
    "        else:\n",
    "            print('选择正确操作！')\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2ab3545-beb7-4425-8ae7-727e85f6f2b4",
   "metadata": {},
   "source": [
    "## 2.5、补充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cb7fe81b-417f-4b3e-8653-44128b2cc8b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['a', 'b'])\n",
      "a:1\n",
      "b:2\n",
      "None\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "#创建字典\n",
    "d = dict()\n",
    "d = {'a':1,'b':2}\n",
    "print(d.keys())\n",
    "for k in d.keys():\n",
    "    print(f\"{k}:{d[k]}\")\n",
    "# print(d['aa']) 会报错\n",
    "print(d.get('aa')) #不存在返回none\n",
    "print(d.get('a')) #存在返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2b8a35d-bb9b-4230-9457-b7c852788a56",
   "metadata": {},
   "source": [
    "## 2.6、元组\n",
    "元组类似列表，但不可变，用()创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "062442a0-91bc-4304-a92d-e325dbff5b2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "160899584 <class 'tuple'>\n",
      "(1, 2, 'a', 3)\n"
     ]
    }
   ],
   "source": [
    "t = (1,2,'a',3)\n",
    "print(id(t),type(t))\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "34dbcddd-3759-432d-895b-8ceccc599edd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 1, '2')\n",
      "('b', 2, 5)\n",
      "('c', 3, 'g')\n"
     ]
    }
   ],
   "source": [
    "l1 = ['a','b','c']\n",
    "l2 = [1,2,3]\n",
    "l3 = ['2',5,'g']\n",
    "tt = zip(l1,l2,l3)\n",
    "for t in tt:\n",
    "    print(t)\n",
    "# zip()放的是元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7decca0-3849-45d0-8385-ed1785294922",
   "metadata": {},
   "source": [
    "## 2.7、字典生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fe2525f9-1393-4b29-b01f-b486716b2295",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 3}\n"
     ]
    }
   ],
   "source": [
    "lst1 = ['a', 'b', 'c']\n",
    "lst2 = [1, 2, 3]\n",
    "d = {k:v for k,v in zip(lst1, lst2)}\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75101e17-7bf4-40ab-b829-a778fedc835f",
   "metadata": {},
   "source": [
    "## 2.8、集合\n",
    "集合set用{}创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "22daaf89-0605-4607-a8b2-8ecc258d5a68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a', 3, 12, 4}\n"
     ]
    }
   ],
   "source": [
    "s = {12,3,'a',4}\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e7cdfbb7-b985-4233-a87e-9451dadaf729",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{32, 2, 4, 6, 8, 90}\n",
      "{32, 2, 4, 6, 122, 8, 90}\n",
      "{32, 2, 4, 6, 8, 90}\n",
      "{32, 4, 6, 8, 90}\n"
     ]
    }
   ],
   "source": [
    "s = {32,4,2,6,8,90}\n",
    "print(s)\n",
    "s.add(122)#添加元素\n",
    "print(s)\n",
    "s.remove(122)#移除当前元素，不存在会报错\n",
    "print(s)\n",
    "s.discard(2)#也是移除当前元素，不会报错\n",
    "print(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b8c3232f-59b7-4b1d-b943-61fbb9ce98d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "s1 = {32,4,2,6,8,90}\n",
    "s2 = {32,4,2,6,8,90,80}\n",
    "print(s1.issubset(s2))\n",
    "print(s2.issuperset(s1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c988934-8ac2-492f-837a-a3c50c3e4036",
   "metadata": {},
   "source": [
    "# 三、函数\n",
    "## 3.1、函数基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ddedd08d-28df-4a84-8493-d2c1905972f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "def add(a,b):\n",
    "    return a+b\n",
    "c = add(1,2)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "3c4e1a9a-6ec8-4aa9-97b5-0555eb0f3f79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 20\n"
     ]
    }
   ],
   "source": [
    "def swap(a,b):\n",
    "    a,b = b,a\n",
    "a = 10\n",
    "b = 20\n",
    "swap(a,b)\n",
    "print(a,b)\n",
    "#说明函数传参为不可变"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "025d490c-8100-42a3-aab3-28df30c53ec9",
   "metadata": {},
   "source": [
    "## 3.2、函数的其他写法\n",
    "函数可以有默认值，即如果不传就是定义的默认值，传参就是传过去的值,且默认参数只能放在最后，且可以有多个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8c930ce6-f296-4179-8a62-3ec85ef4bff5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "def fn1(a,b,c=10):\n",
    "    return a+b+c\n",
    "r1 = fn1(1,2)\n",
    "print(r1)\n",
    "r2 = fn1(1,2,3)\n",
    "print(r2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4a2c7b0f-1090-4dd1-9e7b-c2000a7e6b70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4)\n",
      "10\n",
      "(1, 2, 3, 4, 5)\n",
      "15\n",
      "(2, 2, 2, 2)\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "def fn1(*arr):\n",
    "    sum = 0\n",
    "    print(arr)\n",
    "    for i in arr:\n",
    "        sum += i\n",
    "    return sum\n",
    "a = [1,2,3,4]\n",
    "print(fn1(*a))\n",
    "b = (1,2,3,4,5)\n",
    "print(fn1(*b))\n",
    "print(fn1(2,2,2,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "a37b4ac5-1e4d-4156-906e-a712458205d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 4}\n"
     ]
    }
   ],
   "source": [
    "def fn1(**dic):\n",
    "    print(dic)\n",
    "fn1(a=1,b=2,c=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83045b98-e398-42aa-bb1c-6914b442ad93",
   "metadata": {},
   "source": [
    "## 3.3、管理系统1.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "0883ca61-8c69-47ea-bcca-8aa33e52eb6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "welcome!\n",
      "\n",
      "选择操作：添加A, 删除D, 更新U, 查询工号Q, 查询姓名N, 列表L, 退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 a\n",
      "输入工号 1\n",
      "输入姓名 aa\n",
      "输入年龄 2\n",
      "输入岗位 s\n",
      "输入薪水 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加成功\n",
      "\n",
      "选择操作：添加A, 删除D, 更新U, 查询工号Q, 查询姓名N, 列表L, 退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 a\n",
      "输入工号 2\n",
      "输入姓名 abc\n",
      "输入年龄 2\n",
      "输入岗位 s\n",
      "输入薪水 4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加成功\n",
      "\n",
      "选择操作：添加A, 删除D, 更新U, 查询工号Q, 查询姓名N, 列表L, 退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 a\n",
      "输入工号 3\n",
      "输入姓名 c\n",
      "输入年龄 3\n",
      "输入岗位 c\n",
      "输入薪水 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加成功\n",
      "\n",
      "选择操作：添加A, 删除D, 更新U, 查询工号Q, 查询姓名N, 列表L, 退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 q\n",
      "输入查询方式：1:按工号查询，2:按姓名查询 2\n",
      "输入要查询的姓名 a\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "工号      姓名      年龄      岗位      薪水      \n",
      "1         aa      2         s       2       \n",
      "2         abc     2         s       4       \n",
      "\n",
      "选择操作：添加A, 删除D, 更新U, 查询工号Q, 查询姓名N, 列表L, 退出E\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入 e\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bye\n"
     ]
    }
   ],
   "source": [
    "f_title = \"{:<8}{:<8}{:<8}{:<8}{:<8}\"\n",
    "f_data = \"{:<10}{:<8}{:<10}{:<8}{:<8}\"\n",
    "emps = []\n",
    "\n",
    "def getemp(nu):\n",
    "    for e in emps:\n",
    "        if e['no'] == nu:\n",
    "            return e\n",
    "    return None\n",
    "\n",
    "def getempn(name):\n",
    "    arr = []\n",
    "    for e in emps:\n",
    "        if e['name'].find(name) != -1:\n",
    "            arr.append(e)\n",
    "    return arr\n",
    "\n",
    "def addemp():\n",
    "    nu = int(input(\"输入工号\"))\n",
    "    em = getemp(nu)\n",
    "    if em:\n",
    "        print(\"员工已存在！\")\n",
    "        return\n",
    "    name = input(\"输入姓名\")\n",
    "    age = int(input(\"输入年龄\"))\n",
    "    ob = input(\"输入岗位\")\n",
    "    mo = int(input(\"输入薪水\"))\n",
    "    emps.append({'no': nu, 'name': name, 'age': age, 'major': ob, 'salary': mo})\n",
    "    print('添加成功')\n",
    "\n",
    "def delemp():\n",
    "    nu = int(input(\"输入要删除的工号\"))\n",
    "    em = getemp(nu)\n",
    "    if em:\n",
    "        emps.remove(em)\n",
    "        print('删除成功！')\n",
    "    else:\n",
    "        print('员工不存在！')\n",
    "\n",
    "def searchemp():\n",
    "    n = int(input('输入查询方式：1:按工号查询，2:按姓名查询'))\n",
    "    if n == 1:\n",
    "        nu = int(input('输入要查询的工号'))\n",
    "        em = getemp(nu)\n",
    "        if em:\n",
    "            print(f_title.format('工号', '姓名', '年龄', '岗位', '薪水'))\n",
    "            print(f_data.format(em['no'], em['name'], em['age'], em['major'], em['salary']))\n",
    "        else:\n",
    "            print(\"员工不存在！\")\n",
    "    elif n == 2:\n",
    "        name = input('输入要查询的姓名')\n",
    "        l = getempn(name)\n",
    "        if l:\n",
    "            print(f_title.format('工号', '姓名', '年龄', '岗位', '薪水'))\n",
    "            for e in l:\n",
    "                print(f_data.format(e['no'], e['name'], e['age'], e['major'], e['salary']))\n",
    "        else:\n",
    "            print('员工不存在！')\n",
    "    else:\n",
    "        print('请选择正确操作！')\n",
    "\n",
    "def changeemp():\n",
    "    nu = int(input('输入要更改的工号'))\n",
    "    em = getemp(nu)\n",
    "    if em:\n",
    "        emps.remove(em)\n",
    "        name = input(\"输入姓名\")\n",
    "        age = int(input(\"输入年龄\"))\n",
    "        ob = input(\"输入岗位\")\n",
    "        mo = int(input(\"输入薪水\"))\n",
    "        emps.append({'no': nu, 'name': name, 'age': age, 'major': ob, 'salary': mo})\n",
    "        print('更改成功！')\n",
    "    else:\n",
    "        print(\"员工不存在！\")\n",
    "\n",
    "def listemp():\n",
    "    if not emps:\n",
    "        print(\"暂无员工信息\")\n",
    "        return\n",
    "    print(f_title.format('工号', '姓名', '年龄', '岗位', '薪水'))\n",
    "    for e in emps:\n",
    "        print(f_data.format(e['no'], e['name'], e['age'], e['major'], e['salary']))\n",
    "\n",
    "def main():\n",
    "    print('welcome!')\n",
    "    while True:\n",
    "        print('\\n选择操作：添加A, 删除D, 更新U, 查询工号Q, 查询姓名N, 列表L, 退出E')\n",
    "        op = input(\"输入\").strip().upper()\n",
    "        if op == 'A':\n",
    "            addemp()\n",
    "        elif op == 'D':\n",
    "            delemp()\n",
    "        elif op == 'U':\n",
    "            changeemp()\n",
    "        elif op == 'Q':\n",
    "            searchemp()\n",
    "        elif op == 'N':\n",
    "            searchemp_by_name()\n",
    "        elif op == 'L':\n",
    "            listemp()\n",
    "        elif op == 'E':\n",
    "            print(\"bye\")\n",
    "            break\n",
    "        else:\n",
    "            print('请选择正确操作！')\n",
    "\n",
    "main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70c4f932-ca68-49dc-8649-c7ae8dbe55fa",
   "metadata": {},
   "source": [
    "# 四、字符串\n",
    "## 4.1、字符串基础操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "529ca7f9-8a07-49fc-9ce1-1e54ed144194",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n",
      "e\n",
      "h e l l o   w o r l d "
     ]
    }
   ],
   "source": [
    "s = 'hello world'\n",
    "print(len(s))\n",
    "print(s[1])\n",
    "for i in s:\n",
    "    print(i,end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3094bbd1-f498-4eb0-a680-313432e32d96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".txt\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "s = 'hello world.txt'\n",
    "p = s.rfind('.')# rfind从右边查找\n",
    "e1 = s[p:]\n",
    "print(e1)\n",
    "print(s.endswith(e1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "99319630-e065-4d8f-8452-d0d25f3793d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'a', 'b', '123']\n",
      "['hell', ' w', 'rld a b 123']\n"
     ]
    }
   ],
   "source": [
    "s = 'hello world a b 123'\n",
    "lst = s.split() #默认按空格分割\n",
    "print(lst)\n",
    "l2 = s.split('o') #也可写作sep='o'\n",
    "print(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5cf7fadc-f10b-43fc-8798-46d8347f7211",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D:/a/b/c', 'd.txt']\n"
     ]
    }
   ],
   "source": [
    "fpath = 'D:/a/b/c/d.txt'\n",
    "l = fpath.rsplit('/',maxsplit=1)\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f7d60ad2-a8d1-45d0-9222-982e31bdbdbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hell666 w666rld a b 123\n"
     ]
    }
   ],
   "source": [
    "s = 'hello world a b 123'\n",
    "s1 = s.replace('o','666')\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a96c35b-c9c6-4c8f-8cef-329ed2e260bf",
   "metadata": {},
   "source": [
    "## 4.2、字符串切片\n",
    "和列表操作完全一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "5a545bd3-3876-4198-ad08-fa8c8ea329f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dlrow olleh\n",
      "lo le\n",
      "hlowr\n"
     ]
    }
   ],
   "source": [
    "s = 'hello world'\n",
    "print(s[::-1])\n",
    "print(s[1::2][::-1])\n",
    "print(s[-3::-2][::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0765cdea-d4df-445b-b82e-07d06c0f968f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. False\n",
      "2. True\n",
      "3. True\n",
      "4. True\n",
      "5. True\n",
      "6. False\n",
      "7. True\n",
      "8. True\n",
      "9. True\n",
      "10. a   a\n"
     ]
    }
   ],
   "source": [
    "s = 'hello,python'\n",
    "\n",
    "# 1. 判断是否是合法的标识符（变量名） -> False，因为有逗号\n",
    "print('1.', s.isidentifier())\n",
    "\n",
    "# 2. 判断是否是合法的标识符 -> True\n",
    "print('2.', 'hello'.isidentifier())\n",
    "\n",
    "# 3. 判断是否是合法的标识符（中文+下划线） -> True（Python 支持中文做变量名）\n",
    "print('3.', '寮犱笁_'.isidentifier())\n",
    "\n",
    "# 4. 判断是否全是空格 -> True\n",
    "print('4.', '     '.isspace())\n",
    "\n",
    "# 5. 判断是否全部为字母 -> True\n",
    "print('5.', 'abcAA'.isalpha())\n",
    "\n",
    "# 6. 判断是否全部为字母（有 . 不符合） -> False\n",
    "print('6.', 'abc.'.isalpha())\n",
    "\n",
    "# 7. 判断是否全部是十进制数字 -> True\n",
    "print('7.', '123'.isdecimal())\n",
    "\n",
    "# 8. 判断是否是数字（包括中文数字）-> True\n",
    "print('8.', '一二三四五六'.isnumeric())\n",
    "\n",
    "# 9. 判断是否全是字母或数字 -> True\n",
    "print('9.', 'abc123'.isalnum())\n",
    "\n",
    "# 10. 去除前后空格 -> 'a   a'\n",
    "s1 = '   a   a   '\n",
    "print('10.', s1.strip())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ee882c0-9d1d-4cdb-874d-9120c5d0808d",
   "metadata": {},
   "source": [
    "# 五、面向对象\n",
    "## 5.1、面向对象基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e2bc256c-db76-4617-b61e-51e6c739c239",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3]\n",
    "print(type(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a57e35bc-bbc7-4f32-be48-e65070b8f151",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张涛嘞嘞嘞！\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "class Dog:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def bark(self):\n",
    "        print(f'{self.name}嘞嘞嘞！')\n",
    "\n",
    "d = Dog('张涛',30)\n",
    "d.bark()\n",
    "print(d.age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "335983e4-056c-4678-84ed-7aaf44adabdc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "109228272 <class 'type'>\n",
      "152824736 <class '__main__.Person'>\n",
      "张三:22\n",
      "张三:22\n",
      "张三:33\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def talk(self):\n",
    "        print(f\"{self.name}:{self.age}\")\n",
    "        \n",
    "print(id(Person),type(Person))\n",
    "p1 = Person(\"张三\",22)\n",
    "print(id(p1),type(p1))\n",
    "p2 = p1\n",
    "p1.talk()\n",
    "p2.talk()\n",
    "p2.age = 33\n",
    "p1.talk()\n",
    "#类是一个可变类型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "05b72658-1e49-4581-acc0-cf613771283b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "109234224 <class 'type'>\n",
      "152820032 <class '__main__.Person'>\n",
      "寮犱笁:22\n",
      "鏉庡洓:55\n",
      "16\n",
      "16\n",
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name', 'talk']\n",
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'ach', 'age', 'name', 'talk']\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def talk(self):\n",
    "        print(f\"{self.name}:{self.age}\")\n",
    "        \n",
    "# 打印类的内存地址和类型\n",
    "print(id(Person), type(Person))\n",
    "\n",
    "# 创建对象 p1\n",
    "p1 = Person(\"寮犱笁\", 22)\n",
    "print(id(p1), type(p1))\n",
    "\n",
    "# p2 指向和 p1 同一个对象\n",
    "p2 = p1\n",
    "\n",
    "# 创建新对象 p3\n",
    "p3 = Person(\"鏉庡洓\", 33)\n",
    "p3.age = 55  # 修改属性\n",
    "\n",
    "# 调用方法\n",
    "p1.talk()\n",
    "p3.talk()\n",
    "\n",
    "# 给 p1 添加动态属性\n",
    "p1.ach = 16\n",
    "print(p1.ach)\n",
    "print(p2.ach)\n",
    "\n",
    "# p3 没有 ach 属性，访问会报错\n",
    "# print(p3.ach)\n",
    "\n",
    "# 查看对象属性\n",
    "print(dir(p3))\n",
    "print(dir(p2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d83099c-1856-4c00-9f32-88faceda5e1d",
   "metadata": {},
   "source": [
    "## 5.2、对象的继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102c189d-08a4-49f1-ae8f-873320fae3d2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b7a80bc-bd0d-4c9e-8e11-80bdcbc9c39f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "00018209-d790-4de0-86b7-22891d5fc190",
   "metadata": {},
   "source": [
    "## 5.3、电脑组装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ad92333c-959e-46d6-a796-1ef5134e87ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "芜湖电脑 is run\n",
      "祖冲之号114514核心--->AMD\n",
      "RTX9090ti--->2048TB\n",
      "芜湖！！！\n"
     ]
    }
   ],
   "source": [
    "class CPU:\n",
    "    def __init__(self,name,_type):\n",
    "        self.name = name\n",
    "        self._type = _type\n",
    "    def run(self): \n",
    "        print(f\"{self.name}--->{self._type}\")\n",
    "\n",
    "class GPU:\n",
    "    def __init__(self,name,size):\n",
    "        self.name = name\n",
    "        self.size = size\n",
    "    def run(self): \n",
    "        print(f\"{self.name}--->{self.size}\")\n",
    "\n",
    "class MainBoard: \n",
    "    def __init__(self,name,cpu,gpu): \n",
    "        self.name = name\n",
    "        self.cpu = cpu\n",
    "        self.gpu = gpu\n",
    "    def startup(self): \n",
    "        print(f\"{self.name} is run\")\n",
    "        self.cpu.run()\n",
    "        self.gpu.run()\n",
    "        print('芜湖！！！')\n",
    "\n",
    "c = CPU('祖冲之号114514核心','AMD')\n",
    "g = GPU('RTX9090ti','2048TB')\n",
    "m = MainBoard('芜湖电脑',c,g)\n",
    "m.startup()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f878eaaa-57cf-4331-b45c-0ba8c53e1860",
   "metadata": {},
   "source": [
    "## 5.4、深拷贝和浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "daf6d080-cb9f-49df-821f-7cf6a5e8d077",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "芜湖电脑 is run\n",
      "祖冲之号114514核心--->AMD\n",
      "RTX9090ti--->2048TB\n",
      "芜湖！！！\n",
      "12407472 12407472 16138960\n",
      "12407136 12407136\n",
      "10635088 10635088\n",
      "12407136 92439888\n"
     ]
    }
   ],
   "source": [
    "class CPU:\n",
    "    def __init__(self,name,_type):\n",
    "        self.name = name\n",
    "        self._type = _type\n",
    "    def run(self): \n",
    "        print(f\"{self.name}--->{self._type}\")\n",
    "\n",
    "class GPU:\n",
    "    def __init__(self,name,size):\n",
    "        self.name = name\n",
    "        self.size = size\n",
    "    def run(self): \n",
    "        print(f\"{self.name}--->{self.size}\")\n",
    "\n",
    "class MainBoard: \n",
    "    def __init__(self,name,cpu,gpu): \n",
    "        self.name = name\n",
    "        self.cpu = cpu\n",
    "        self.gpu = gpu\n",
    "    def startup(self): \n",
    "        print(f\"{self.name} is run\")\n",
    "        self.cpu.run()\n",
    "        self.gpu.run()\n",
    "        print('芜湖！！！')\n",
    "\n",
    "cp = CPU('祖冲之号114514核心','AMD')\n",
    "g = GPU('RTX9090ti','2048TB')\n",
    "m = MainBoard('芜湖电脑',cp,g)\n",
    "m.startup()\n",
    "\n",
    "from copy import copy as c #浅拷贝，只拷贝表层\n",
    "from copy import deepcopy as dc #深拷贝，整个连同内层都拷贝\n",
    "m2 = m\n",
    "m3 = c(m)\n",
    "print(id(m),id(m2),id(m3))\n",
    "print(id(m.cpu),id(m3.cpu))\n",
    "print(id(m.cpu.name),id(m3.cpu.name))\n",
    "m4 = dc(m)\n",
    "print(id(m.cpu),id(m4.cpu))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "bb04ef7e-378c-4b34-9899-83ccf9360756",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new调用，cls的id为69382288\n",
      "object被创建，id152954800\n",
      "init被调用，id152954800\n",
      "152954800 <class '__main__.Dog'>\n",
      "69382288 <class 'type'>\n",
      "new调用，cls的id为69382288\n",
      "object被创建，id9771664\n",
      "init被调用，id9771664\n",
      "152954800 <class '__main__.Dog'>\n",
      "----------------------------------------\n",
      "new调用，cls的id为69382288\n",
      "object被创建，id9778384\n"
     ]
    }
   ],
   "source": [
    "class Dog(object):\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        print(f\"init被调用，id{id(self)}\")\n",
    "    def __new__(cls,*args,**kwargs):\n",
    "        print(f\"new调用，cls的id为{id(cls)}\")\n",
    "        obj = super().__new__(cls)\n",
    "        print(f\"object被创建，id{id(obj)}\")\n",
    "        return obj\n",
    "    \n",
    "    def bark(self):\n",
    "        print(f'{self.name}嘞嘞嘞！')\n",
    "\n",
    "d = Dog('张涛',30)\n",
    "print(id(d),type(d))\n",
    "print(id(Dog),type(Dog))\n",
    "d2 = Dog('666',114)\n",
    "print(id(d),type(d))\n",
    "print('-'*40)\n",
    "import copy\n",
    "d3 = copy.copy(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7f533223-2384-4dd9-bee2-2a63bb838908",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'__module__': '__main__', '__firstlineno__': 1, '__init__': <function Dog.__init__ at 0x0000000007A7E020>, 'bark': <function Dog.bark at 0x0000000007A7E3E0>, '__static_attributes__': ('age', 'name'), '__dict__': <attribute '__dict__' of 'Dog' objects>, '__weakref__': <attribute '__weakref__' of 'Dog' objects>, '__doc__': None}\n",
      "--------------------------------------------------------------------------------\n",
      "{'name': '张涛', 'age': 30}\n"
     ]
    }
   ],
   "source": [
    "class Dog:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def bark(self):\n",
    "        print(f'{self.name}嘞嘞嘞！')\n",
    "\n",
    "d = Dog('张涛',30)\n",
    "print(Dog.__dict__)\n",
    "print('--'*40)\n",
    "print(d.__dict__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "0c9175d4-e1aa-4b0a-956a-5c44e207294e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'__module__': '__main__', '__firstlineno__': 4, '__init__': <function Dog.__init__ at 0x0000000007BBE3E0>, '__str__': <function Dog.__str__ at 0x0000000007BBE840>, '__eq__': <function Dog.__eq__ at 0x0000000007BBFD80>, '__add__': <function Dog.__add__ at 0x0000000007BBEDE0>, '__static_attributes__': ('age', 'name'), '__doc__': None, '__hash__': None}\n",
      "--------------------------------------------------------------------------------\n",
      "{'base': 'animal', 'name': '旺财', 'age': 3}\n",
      "--------------------------------------------------------------------------------\n",
      "animal\n",
      "--------------------------------------------------------------------------------\n",
      "<bound method Dog.__str__ of <__main__.Dog object at 0x00000000091DC2F0>>\n",
      "--------------------------------------------------------------------------------\n",
      "name:旺财,age:3\n",
      "False\n",
      "True\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "class Animal: \n",
    "    def __init__(self): \n",
    "        self.base = \"animal\"\n",
    "class Dog(Animal):\n",
    "    def __init__(self,name,age): \n",
    "        super().__init__() #调用父类的初始化方法\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    def __str__(self):\n",
    "        return f'name:{self.name},age:{self.age}'\n",
    "    def __eq__(self,obj): \n",
    "        return self.name == obj.name and self.age == obj.age\n",
    "    def __add__(self,obj):\n",
    "        return self.age + obj.age\n",
    "print(Dog.__dict__)\n",
    "d = Dog('旺财',3)\n",
    "print('--'*40)\n",
    "print(d.__dict__)#查询自己的值\n",
    "print('--'*40)\n",
    "print(d.base)\n",
    "print('--'*40)\n",
    "print(d.__str__)\n",
    "print('--'*40)\n",
    "print(d.__str__())\n",
    "d2 = Dog('旺财',5)\n",
    "d3 = Dog('旺财',3)\n",
    "print(d.__eq__(d2))\n",
    "print(d.__eq__(d3))\n",
    "print(d+d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9275fcec-0e62-41b4-89f1-874c4355fce5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
